package com.customer.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.customer.mall.domain.Category;
import com.customer.mall.domain.vo.ProductVO;
import com.customer.mall.mapper.ProductMapper;
import com.customer.mall.domain.Product;
import com.customer.mall.service.ICategoryService;
import com.customer.mall.service.IProductService;
import com.customer.mall.util.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author BaiGGG
 * @since 2023-06-25
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Autowired
    private ICategoryService categoryService;

    @Override
    public ResponseResult<Page<ProductVO>> pageProduct(String name, String categoryName, Integer pageNum, Integer pageSize) {
        Page<Product> productPage = new Page<>(pageNum, pageSize);
        Page<ProductVO> productVOPage = new Page<>();
        Integer getCategoryId = null;
        if (StringUtils.hasText(categoryName)) {
            Category getCategory = categoryService.getOne(new QueryWrapper<Category>().select("id").eq("name", categoryName));
            if(!Objects.isNull(getCategory)) {
                getCategoryId = getCategory.getId();
            }
        }
        page(productPage, new QueryWrapper<Product>()
                .eq(!Objects.isNull(getCategoryId) , "category_id", getCategoryId)
                .like(StringUtils.hasText(name), "title", name));
        productVOPage.setRecords(productPage.getRecords().stream().map(item -> {
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(item, productVO);
            productVO.setCategoryName(categoryService.getById(item.getCategoryId()).getName());
            return productVO;
        }).collect(Collectors.toList()));
        BeanUtils.copyProperties(productPage, productVOPage, "records");
        return ResponseResult.okResult(productVOPage);
    }

    @Override
    public ResponseResult<ProductVO> getProductById(Integer id) {
        Product getProduct = getById(id);
        if (Objects.isNull(getProduct)) {
            return ResponseResult.failResult();
        }
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(getProduct, productVO);
        productVO.setCategoryName(categoryService.getById(productVO.getCategoryId()).getName());
        return ResponseResult.okResult(productVO);
    }

    @Override
    public ResponseResult<String> insertProduct(Product product) {
        String code = product.getCode();
        Product getProduct = getOne(new QueryWrapper<Product>().eq("code", code));
        if (!Objects.isNull(getProduct)) {
            return ResponseResult.failResult("此状态码已存在");
        }
        if (!save(product)) {
            return ResponseResult.failResult("保存失败");
        }
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult<String> updateProduct(Product product) {
        String code = product.getCode();
        Product getProduct = getOne(new QueryWrapper<Product>().eq(StringUtils.hasText(code), "code", code).ne("id", product.getId()));
        if (!Objects.isNull(getProduct)) {
            return ResponseResult.failResult("此状态码已存在");
        }
        if (!updateById(product)) {
            return ResponseResult.failResult("更新失败");
        }
        return ResponseResult.okResult();
    }

}
